{% set pypi_name = 'freezer' %}
{% set source = fetch_source('https://tarballs.openstack.org/freezer/freezer-master.tar.gz') %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}
%if 0%{?rhel} || 0%{?fedora}
%global rdo 1
%endif
Name:           {{ py2name() }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Summary:        OpenStack Backup and Restore
License:        {{ license('Apache-2.0') }}
Group:          Development/Languages/Python
URL:            https://docs.openstack.org/freezer
Source0:        {{ source|basename }}
Source1:        openstack-freezer.logrotate
Source2:        openstack-freezer-scheduler.service
BuildRequires:  openstack-macros
BuildRequires:  {{ py2pkg('tempest') }}
BuildRequires:  {{ py3('APScheduler') }}
BuildRequires:  {{ py3('Babel') }}
BuildRequires:  {{ py3('PyMySQL') }}
BuildRequires:  {{ py3('botocore') }}
BuildRequires:  {{ py3('python-cinderclient') }}
BuildRequires:  {{ py3('cryptography') }}
BuildRequires:  {{ py3('ddt') }}
BuildRequires:  {{ py3('fixtures') }}
BuildRequires:  {{ py3('python-freezerclient') }}
BuildRequires:  {{ py3('python-glanceclient') }}
BuildRequires:  {{ py3('keystoneauth1') }}
BuildRequires:  {{ py3('python-keystoneclient') }}
BuildRequires:  {{ py3('mock') }}
BuildRequires:  {{ py3('python-neutronclient') }}
BuildRequires:  {{ py3('python-novaclient') }}
BuildRequires:  {{ py3('os-brick') }}
BuildRequires:  {{ py3('oslo.config') }}
BuildRequires:  {{ py3('oslo.log') }}
BuildRequires:  {{ py3('oslo.service') }}
BuildRequires:  {{ py3('oslo.utils') }}
BuildRequires:  {{ py3('paramiko') }}
BuildRequires:  {{ py3('pbr') }}
BuildRequires:  {{ py3('psutil') }}
BuildRequires:  {{ py3('pymongo') }}
BuildRequires:  {{ py3('setuptools') }}
BuildRequires:  {{ py3('six') }}
BuildRequires:  {{ py3('stestr') }}
BuildRequires:  {{ py3('python-swiftclient') }}
Requires:       python3-{{ pypi_name }} = %{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  systemd-rpm-macros
Requires(pre):  pwdutils
%{?systemd_requires}
%endif
%if 0%{?rdo}
BuildRequires:  systemd
Requires(post): systemd
Requires(postun): systemd
Requires(pre):  shadow-utils
Requires(preun): systemd
%endif

%description
OpenStack Swift incremental backup and restore automation tool for
file system, MongoDB, MySQL. LVM snapshot and encryption support.

%package -n     python3-{{ pypi_name }}
Summary:        Freezer Python libraries
Group:          Applications/System
Requires:       {{ py3('APScheduler') }}
Requires:       {{ py3('Babel') }}
Requires:       {{ py3('PyMySQL') }}
Requires:       {{ py3('botocore') }}
Requires:       {{ py3('python-cinderclient') }}
Requires:       {{ py3('cryptography') }}
Requires:       {{ py3('python-freezerclient') }}
Requires:       {{ py3('python-glanceclient') }}
Requires:       {{ py3('keystoneauth1') }}
Requires:       {{ py3('python-keystoneclient') }}
Requires:       {{ py3('python-neutronclient') }}
Requires:       {{ py3('python-novaclient') }}
Requires:       {{ py3('os-brick') }}
Requires:       {{ py3('oslo.config') }}
Requires:       {{ py3('oslo.log') }}
Requires:       {{ py3('oslo.service') }}
Requires:       {{ py3('oslo.utils') }}
Requires:       {{ py3('paramiko') }}
Requires:       {{ py3('psutil') }}
Requires:       {{ py3('pymongo') }}
Requires:       {{ py3('six') }}
Requires:       {{ py3('python-swiftclient') }}

%description -n python3-{{ pypi_name }}
OpenStack Swift incremental backup and restore automation tool for
file system, MongoDB, MySQL. LVM snapshot and encryption support.

This package contains the Freezer python libraries.

%package agent
Summary:        OpenStack Freezer - Agent service
Group:          Applications/System
Requires:       %{name} = %{version}-%{release}

%description agent
OpenStack Swift incremental backup and restore automation tool for
file system, MongoDB, MySQL. LVM snapshot and encryption support.

This package contains the Freezer agent code.

%package scheduler
Summary:        OpenStack Freezer - Scheduler service
Group:          Applications/System
Requires:       %{name} = %{version}-%{release}

%description scheduler
OpenStack Swift incremental backup and restore automation tool for
file system, MongoDB, MySQL. LVM snapshot and encryption support.

This package contains the Freezer scheduler agent.

%package doc
Summary:        Documentation for OpenStack Backup Service
Group:          Documentation/HTML
BuildRequires:  fdupes
BuildRequires:  {{ py3('Sphinx') }}
BuildRequires:  {{ py3('openstackdocstheme') }}
BuildRequires:  {{ py3('sphinxcontrib-apidoc') }}

%description doc
OpenStack Swift incremental backup and restore automation tool for
file system, MongoDB, MySQL. LVM snapshot and encryption support.

This package contains the documentation

%prep
%autosetup -p1 -n {{ pypi_name }}-{{upstream_version}}
%py_req_cleanup

%build
%{py3_build}

PBR_VERSION={{ upstream_version }} %sphinx_build -b html doc/source doc/build/html
# remove the Sphinx-build leftovers
rm -rf doc/build/html/.{doctrees,buildinfo}
%fdupes doc/build/html/
# config file generation
PYTHONPATH=. oslo-config-generator --config-file etc/config-generator.conf \
    --output-file etc/scheduler.conf.sample

%install
%{py3_install}
install -d -m 755 %{buildroot}%{_sysconfdir}/freezer
install -d -m 755 %{buildroot}%{_localstatedir}/{lib,log}/freezer
install -d -m 755 %{buildroot}%{_sysconfdir}/freezer/freezer.conf.d/
install -d -m 750 %{buildroot}%{_sysconfdir}/freezer/scheduler/
install -d -m 750 %{buildroot}%{_sysconfdir}/freezer/scheduler/conf.d/
install -p -D -m 640 etc/scheduler.conf.sample %{buildroot}%{_sysconfdir}/freezer/scheduler.conf
install -p -D -m 644 %{SOURCE1} %{buildroot}%{_sysconfdir}/logrotate.d/openstack-freezer
install -p -D -m 755 %{SOURCE2} %{buildroot}%{_unitdir}/%{name}-scheduler.service
# install symlinks on SUSE
%if 0%{?suse_version}
mkdir -p %{buildroot}%{_sbindir}
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}-scheduler
%endif

# remove duplicates
%fdupes %{buildroot}%{python3_sitelib}

# remove Windows script
rm -fr %{buildroot}%{_prefix}/freezer/scripts/vss.ps1

%pre
%openstack_pre_user_group_create freezer freezer /sbin/nologin

#POST actions
%post scheduler
%systemd_post %{name}-scheduler.service

#PREUN actions
%preun scheduler
%systemd_preun %{name}-scheduler.service

#POSTUN actions
%postun scheduler
%systemd_postun_with_restart %{name}-scheduler.service

%check
export OS_TEST_PATH=./freezer/tests/unit
%if 0%{?suse_version}
PYTHON=python3 stestr run
%endif
%if 0%{?rdo}
stestr-3 run
%endif

%files
%license LICENSE
%doc README.rst
%dir %attr(0750, root, freezer) %{_sysconfdir}/freezer/
%dir %attr(0750, root, freezer) %{_sysconfdir}/freezer/freezer.conf.d/
%dir %attr(0750, root, freezer) %{_sysconfdir}/freezer/scheduler/
%dir %attr(0750, root, freezer) %{_sysconfdir}/freezer/scheduler/conf.d/
%config(noreplace) %attr(0640, root, freezer) %{_sysconfdir}/freezer/scheduler.conf
%config(noreplace) %{_sysconfdir}/logrotate.d/openstack-freezer
%dir %attr(0755, freezer, freezer) %{_localstatedir}/lib/freezer
%dir %attr(0750, freezer, freezer) %{_localstatedir}/log/freezer

%files -n python3-{{ pypi_name }}
%doc README.rst
%license LICENSE
%{python3_sitelib}/freezer*
%{python3_sitelib}/freezer-*.egg-info

%files agent
%license LICENSE
%{_bindir}/freezer-agent

%files scheduler
%license LICENSE
%{_bindir}/freezer-scheduler
%{_unitdir}/%{name}-scheduler.service
%if 0%{?suse_version}
%{_sbindir}/rc%{name}-scheduler
%endif

%files doc
%license LICENSE
%doc doc/build/html

%changelog
